package com.xiaojf.xlog.appender.es;

import com.xiaojf.xlog.appender.AccessLogAppender;
import com.xiaojf.xlog.appender.es.entity.AccessLog;
import com.xiaojf.xlog.appender.es.repository.AccessLogRepository;
import com.xiaojf.xlog.model.XlogAccessLog;
import org.springframework.scheduling.annotation.Async;

/**
 * 使用es保存日志信息
 *
 * @author Ade.Xiao 2021-03-16 13:10:27
 */
public class AccessLogEsAppender implements AccessLogAppender {
    private AccessLogRepository accessLogRepository;

    public AccessLogEsAppender(AccessLogRepository accessLogRepository) {
        this.accessLogRepository = accessLogRepository;
        System.out.println("使用 elasticsearch 记录访问日志...");
    }

    @Override
    @Async
    public void record(XlogAccessLog log) {
        AccessLog entity = new AccessLog();
        entity.setAccessTime(log.getAccessTime());
        entity.setAppendTime(log.getAppendTime());
        entity.setContent(log.getContent());
        entity.setOperateType(log.getOperateType().toString());
        entity.setIp(log.getIp());
        entity.setUri(log.getUri());
        entity.setError(log.getError());
        entity.setUri(log.getUri());
        entity.setSuccessful(log.isSuccessful());

        accessLogRepository.save(entity);
    }
}